import React from 'react';
import { PopConfirm, Radio } from '@teamix/ui';
import intl from '../../../../../../../../../../locale';

export default class GcConfirm extends React.Component {
  state = {
    visible: false,
    immediately: false,
  };

  onCancel = () => {
    this.setState({
      visible: false,
      immediately: false,
    });
  };

  onConfirm = () => {
    const { immediately } = this.state;
    this.setState({
      visible: false,
      immediately: false,
    });

    this.props.onChange(immediately);
  };

  onVisibleChange = (visible) => {
    this.setState({
      visible,
    });

    // 关闭恢复数据
    if (!visible) {
      this.setState({
        immediately: false,
      });
    }
  };

  render() {
    const { visible, immediately } = this.state;
    const { trigger, isLoading } = this.props;
    if (isLoading) {
      return trigger;
    }
    return (
      <PopConfirm
        triggerType="click"
        align="tl"
        title={intl.get({
          id: 'code-assets.containers.storageForm.gcConfirm.CleanupPolicy',
          defaultMessage: '清理策略',
        })}
        popupStyle={{
          width: 500,
          maxWidth: 'none',
        }}
        content={
          <div>
            <div className="gc-confirm-info">
              {intl.get({
                id: 'code-assets.containers.storageForm.gcConfirm.GcFreeDataWillContinue',
                defaultMessage: 'GC 的游离数据将继续占用代码库空间，请确认清理策略：',
              })}
            </div>
            <div>
              <Radio.Group
                value={immediately}
                onChange={(value) => {
                  this.setState({
                    immediately: value,
                  });
                }}
              >
                <Radio value={false}>
                  {intl.get({
                    id: 'code-assets.containers.storageForm.gcConfirm.AutomaticallyDeleteTheCacheAfter',
                    defaultMessage: '缓存失效后自动删除（避免误删，缓存 14 天后自动释放空间）',
                  })}
                </Radio>
                <Radio value>
                  {intl.get({
                    id: 'code-assets.containers.storageForm.gcConfirm.DeleteItImmediatelyItCannot',
                    defaultMessage: '立即删除，删除后无法恢复',
                  })}
                </Radio>
              </Radio.Group>
            </div>
          </div>
        }
        onConfirm={this.onConfirm}
        onCancel={this.onCancel}
        visible={visible}
        onVisibleChange={this.onVisibleChange}
        trigger={trigger}
      />
    );
  }
}
